
let form = document.querySelector('form')
let inpName = document.querySelector('input[type=text]')
let inpPwd = document.querySelector('input[type=password]')
let rember = document.querySelector('input[name=rember]')
form.addEventListener('submit', async function (e) {
    e = e || window.event
    e.preventDefault()
    let userName = inpName.value
    let userPassword = inpPwd.value


    if (!userName || !userPassword) {
        return alter('请填写完整表单')
    }
    let res = await pAjax({
        url: '../server/login.php',
        data: { username: userName, password: userPassword },
        method: 'post',
        dataType: "json"
        // let {status,msg}=res.mata
    })
    console.log(res)
    //解构
    let { meta: obj } = res
    let msg = obj.msg
    // 判断登录是否成功
    if (obj.status === 0) {
        //弹出提示
        layer.msg(msg);
        // 清除表单
        form.reset()
        //如果勾选了7天免登入，设置cookie有效期为7天
        if (rember.checked) {
            let options = {}
            options.expires = 7 * 24 * 60 * 60;
            // 调用cookie设置函数
            setCookie('loginStatus', userName, options)
        }
        else{
            setCookie('loginStatus', userName)
        }
       // 跳到首页
        setTimeout(function () {
             location.href = "../page/index.html"
        }, 1500)
    } else {
        layer.msg(msg);
    }
})




